<?php
namespace app\admin\controller;

use app\admin\model\SortModel;

 use think\Controller;
use think\Db;
use think\Request;
use think\Cache;
use think\Cookie;
use think\Session;
class Role extends Common
{


    public function index()
    {
        $db=Db::name("role");
        $pagesize=input('pagesize',0);
        if($pagesize){
            $_SESSION['pagesizes']=$pagesizes=$pagesize;
        }else{
            if(isset($_SESSION['pagesizes'])){
                $pagesizes=$_SESSION['pagesizes'];
            }else{
                $pagesizes=10;
            }
        }
        $list=Db::name("role")->paginate($pagesizes);
        // print_r($list);die;
        $this->assign("list",$list);
        $this->assign("page",0);

        return $this->fetch("/role/index");
    }

    public function add()
    {
        $id=input('id',0);
        $db=Db::name("role");
        $v=$db->find($id);
        if($v){
            $v['role_auth_ids']=explode(",",$v['role_auth_ids']);
        }else{
            $v['role_name']="";
            $v['role_auth_ids']=array();
            $v['status']=1;
            $v['id']='';

        }

        // print_r($v['role_auth_ids']);die;
        if(Request::instance()->isPost()){
            $data=$_POST;

            $auth_ids=implode(",",$_POST['role_auth_ids']);//把数组分割成字符串
            if($auth_ids){
                $pids=$this->role_auth_ids($auth_ids);
                $data['role_auth_ids']=$auth_ids.$pids;
            }

           // print_r($data);die;
            if($id){
                if(Db::name("role")->where("id=".$id)->update($data)){
                    $this->success("修改成功","/admin/role/index.html");
                }else{
                    $this->error("修改失败");
                }
            }else{
                if(Db::name("role")->insert($data)){
                    $this->success("添加成功","/admin/role/index.html");
                }else{
                    $this->error("添加失败");
                }
            }

        }



       // print_r($v);die;
        $this->assign("v",$v);
        return $this->fetch("/role/add");
    }


    public function del()
    {
        $id=input('id',0);
        if(Db::name("role")->delete($id)){
            $this->success("删除成功","/admin/role/index.html");
        }else{
            $this->error("删除失败");
        }



    }

    public function audit()
    {

        $id=input('id',0);
        // echo $id;

        $v=Db::name("role")->find($id);
        $data['status']=$v['status']?0:1;

        if(Db::name("role")->where("id=".$id)->update($data)){
            $this->success("审核成功","/admin/role/index.html");
//            $return_data['status'] = 0;
//            $return_data['msg'] ="审核成功";
        }else{
            $this->error("审核失败");
//            $return_data['status'] = 1;
//            $return_data['msg'] ="审核失败";
        }
//
//        echo json_encode($return_data);


    }
}